﻿using HZH_Controls.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HZH_Controls.Common
{
    public class FieldAliasHelper
    {
        /// <summary>
        /// 设置别名
        /// </summary>
        /// <param name="tabName"></param>
        /// <param name="dtView"></param>
        public void SetAlias(List<FieldViewModel> aliasList,  DataGridView dtView,string BindFieldAlias)
        {
            if (aliasList != null)
            {
                string fieldName = string.Empty;
                for (int index = 0; index < dtView.Columns.Count; index++)
                {
                    fieldName = dtView.Columns[index].Name;
                    var aliasTmp = aliasList.Where(p => p.Field == fieldName).FirstOrDefault();

                    if (aliasTmp != null)
                    {
                        if (fieldName.ToLower() == "name" && !string.IsNullOrEmpty(BindFieldAlias))
                        {
                            dtView.Columns[index].HeaderText = BindFieldAlias;
                        }
                        else
                        {
                            dtView.Columns[index].HeaderText = aliasTmp.Alias;
                        }

                        if (aliasTmp.Rank < dtView.Columns.Count)
                        {
                            dtView.Columns[index].DisplayIndex = aliasTmp.Rank;
                        }
                        else
                        {
                            dtView.Columns[index].DisplayIndex = dtView.Columns.Count - 1;
                        }
                    }
                    else
                    {
                        dtView.Columns[index].Visible = false;
                    }
                }
            }
        }
    }
}
